package com.ruoyi.food.mapper;

import java.sql.Timestamp;
import java.util.List;

import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.ruoyi.food.object.TActOrder;


/**
 * 活动订单mapper
 * @author Administrator
 *
 */
@Mapper
public interface TActOrderMapper extends BaseMapper<TActOrder>{
	
	public static final String sql="SELECT a.ACT_NAME,o.CUSTOMER_NAME,o.M_ORDER_ID,o.HXID,o.ACT_ID,o.PRICE,o.NUMBER,o.UNIT,o.SPEC,o.AMOUNT,o.CREATE_TIME,o.PAY_TYPE,"
			+ "o.SCHOOL,o.GRADE,o.CLASS_NAME,o.CUSTOMER_NAME,o.MOBILE,o.BANK_CARD_NO,o.OR_STATUS,o.BILL_DESC,o.PAY_TIME,o.OPEN_ID,o.UNION_ID "
			+ "from t_act_order o LEFT JOIN t_activity a on o.ACT_ID=a.ACT_ID";
	
	public static final String sql1="SELECT a.ACT_NAME,o.* FROM t_act_order o RIGHT JOIN t_activity a on";
	
	/**
	 * 查询全部的活动订单信息
	 * @return
	 */
	@Select(sql)
	public List<TActOrder> selectAllTActOrder();
	
	/**
	 * 按照条件查询活动订单
	 * @return
	 */
	/*@Select("<script> \n"+sql+' '+
		    "where 1=1 "
		    + "and a.ACT_NAME like concat('%', #{actName}, '%') <if test='actName != null and actName !=\"\"'></if> "
		    + "and u.`NAME` like concat('%', #{name}, '%') <if test='name != null and name!=\"\"'></if>"
		    + "and date_format(o.create_time,'%y%m%d') &gt;= date_format(#{params.beginTime},'%y%m%d') <if test='params.beginTime != null and params.beginTime !=\"\"'></if>"
		    + "and date_format(o.create_time,'%y%m%d') &lt;= date_format(#{params.endTime},'%y%m%d') <if test='params.endTime != null and params.endTime !=\"\"'></if>"
		    + "</script> ")*/
	
	@Select({"<script>",
        sql,
        "WHERE 1=1",
        "<when test='actName != \"\" and actName !=null'>",
        "AND a.ACT_NAME like concat('%', #{actName}, '%')",
        "</when>",
        "<when test='name != \"\" and name !=null'>",
        "AND u.`NAME` like concat('%', #{name}, '%')",
        "</when>",
        "<when test='params.beginTime != \"\" and params.beginTime !=null'>",
        "AND date_format(o.create_time,'%y%m%d') &gt;= date_format(#{params.beginTime},'%y%m%d')",
        "</when>",
        "<when test='params.endTime != \"\" and params.endTime !=null'>",
        "AND date_format(o.create_time,'%y%m%d') &lt;= date_format(#{params.endTime},'%y%m%d')",
        "</when>",
        "<when test='payType != 0'>",
        "AND o.PAY_TYPE=#{payType}",
        "</when>",
        "</script>"})
	public List<TActOrder> selectTActOrderByCon(TActOrder tActOrder);
	
	/**
	 * 通过购买人id查询活动订单
	 * @param clientId
	 * @return
	 */
	@Select("SELECT *from t_act_order where CLIENT_ID=#{clientId} ORDER BY HX_TIME ASC")
	public List<TActOrder> selecTActOrderByClientId(long clientId);
	
	
	@Select("SELECT *from t_act_order where M_ORDER_ID=#{mOrderId}")
	public TActOrder selecTActOrderByMOrderId(String mOrderId);
	
	@Update("update t_act_order set HX_TIME=#{hxTime},HX_ENTERPRISE_ID=#{enterpriseId} where ORDER_ID=#{orderId}")
	public void updatehxTime(@Param("hxTime")Timestamp hxTime,@Param("orderId")long orderId,@Param("enterpriseId")long enterpriseId);
	
	//@Select("SELECT *from t_act_order where MOBILE=#{mobile} and OR_STATUS='TRADE_SUCCESS' ORDER BY HX_TIME,PAY_TIME desc")
	@Select("SELECT a.ACT_NAME,o.* from t_act_order o,t_activity a "
			+ "where MOBILE=#{mobile} and OR_STATUS='TRADE_SUCCESS' and o.ACT_ID=a.ACT_ID "
			+ "and (o.ACT_ID=1 or o.ACT_ID=2) ORDER BY HX_TIME,PAY_TIME desc")
	public List<TActOrder> selecTActOrderByMobile(String mobile);
	
	/**
	 * 根据活动id查询订单列表
	 * @param actIds
	 * @return
	 */
	@Select({"<script>",
        "SELECT a.ACT_NAME,o.* from t_act_order o,t_activity a where o.ACT_ID=a.ACT_ID and o.ACT_ID in",
        "(<foreach collection='actIds' item='id' index='index' separator=','>",
        "#{id}",
        "</foreach>)",
        
        "<when test='tActOrder.actName != \"\" and tActOrder.actName !=null'>",
        "AND a.ACT_NAME like concat('%', #{tActOrder.actName}, '%')",
        "</when>",
        "<when test='tActOrder.name != \"\" and tActOrder.name !=null'>",
        "AND u.`NAME` like concat('%', #{name}, '%')",
        "</when>",
        "<when test='tActOrder.params.beginTime != \"\" and tActOrder.params.beginTime !=null'>",
        "AND date_format(o.create_time,'%y%m%d') &gt;= date_format(#{tActOrder.params.beginTime},'%y%m%d')",
        "</when>",
        "<when test='tActOrder.params.endTime != \"\" and tActOrder.params.endTime !=null'>",
        "AND date_format(o.create_time,'%y%m%d') &lt;= date_format(#{tActOrder.params.endTime},'%y%m%d')",
        "</when>",
        "<when test='tActOrder.payType != 0'>",
        "AND o.PAY_TYPE=#{payType}",
        "</when>",
        "</script>"})
	public List<TActOrder> selecTActOrderByActId(@Param("actIds")List<Long> actIds,@Param("tActOrder")TActOrder tActOrder);
	
}
